var util = require("../../utils/util.js");
Page({
    data: {
        loaddingHidden: true,
        date: new Date().Format("yyyy-MM-dd hh:mm"),
        date1: new Date().Format("yyyy-MM-dd"),
        imagesUrl: [],
        website: '请单击选择网点',
        actionSheetHidden: true,
        actionSheetItems: [{ bindtap: "m1", txt: "删除" }]
    },
    onLoad: function(options) {
        var that = this;
        var websiteid = options.websiteid;
        if (websiteid) {
            that.setData({ website: options.websitename })
            that.setData({ websiteid: options.websiteid })
        }
        util.getLocation(that);
        getAptoticLog(that);
    },
    bindReasonChange: function(e) {
        this.setData({
            reason: e.detail.value
        })
    },
    selectWebSite: function(e) {
        wx.redirectTo({
            url: '../select_webSite_1/select_webSite'
        })
    },
    bindChooseImage: function() {
        var that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                console.log(res)
                    // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                var imagesUrl = new Array();
                for (var i = 0; i < tempFilePaths.length; i++) {
                    imagesUrl[i] = {
                        type: 0,
                        path: tempFilePaths[i]
                    }
                }
                that.setData({ imagesUrl: that.data.imagesUrl.concat(imagesUrl) })
            }
        })
    },
    reserveSubmit: function() {
        var that = this;
        that.setData({ loaddingHidden: !that.data.loaddingHidden })
        var OpenID = wx.getStorageSync("OpenID");
        var Token = wx.getStorageSync("Token");
        var url = getApp().globalData.mainUrl + "WX_MiniApps_HR_AddTimeRecords.ashx";
        var UploadUrl = getApp().globalData.mainUrl + "WX_MiniApps_HR_AddTimeRecords_UploadImg.ashx?1=1";
        UploadUrl = UploadUrl + "&Token=" + Token + "&OpenID=" + OpenID;
        wx.request({
            url: url,
            data: {
                Token: Token,
                OpenID: OpenID,
                GPS_X_Tencent: that.data.latitude,
                GPS_Y_Tencent: that.data.longitude,
                GPSAddress_Tencent: that.data.place.place,
                RecordsType: '2',
                WebSiteID: that.data.websiteid,
                OutWorkReason: that.data.reason
            },
            success: function(res) {
                that.setData({ loaddingHidden: !that.data.loaddingHidden })
                if (res.data.Result == '1') {
                    if (that.data.imagesUrl.length > 0) {
                        UploadUrl = UploadUrl + "&RecordID=" + res.data.ReturnData.RecordID;
                        that.data.imagesUrl.forEach(function(obj, index) {
                            if (obj.type == 0) {
                                wx.uploadFile({
                                    url: UploadUrl,
                                    method: "post",
                                    header: {
                                        "content-type": "multipart/form-data"
                                    },
                                    filePath: obj.path,
                                    name: "Uploadfile",
                                    success: function(res1) {
                                        console.log(res1);
                                    }
                                });
                            }
                        });
                    }

                    wx.showModal({
                        content: res.data.ReturnMsg,
                        title: '提示',
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../index/index'
                                });
                            }
                        }
                    })
                } else {
                    wx.showModal({
                        content: res.data.ReturnMsg,
                        title: '错误'
                    })
                }
            }
        })
    },
    openDeleteMenu: function(e) {
        var that = this;
        this.setData({
            type: e.currentTarget.dataset.type,
            path: e.currentTarget.dataset.path
        });
        wx.showModal({
            title:'删除确认',
            content:'确认删除此内容吗？',
            success:function(res){
                if(res.confirm){
                    deleteContent(that);
                }
            }
        })
    }
})

function deleteContent(that) {
    var type = that.data.type;
    if (type == "image") {
        var imagesUrl_ = new Array();
        that.data.imagesUrl.forEach(function(obj, index) {
            console.log(obj);
            if (obj.path != that.data.path) {
                imagesUrl_[imagesUrl_.length] = obj;
            }
        });
        that.setData({ imagesUrl: imagesUrl_ });
    }
}

//迁到记录
function getAptoticLog(that) {
    var OpenID = wx.getStorageSync("OpenID");
    var Token = wx.getStorageSync("Token");
    var url = getApp().globalData.mainUrl + "WX_MiniApps_HR_GetMemberTimeRecordsList.ashx";
    wx.request({
        url: url,
        data: {
            Token: Token,
            OpenID: OpenID,
            Signtime: new Date().Format("yyyy-MM-dd")
        },
        success: function(res) {
            var list = new Array();
            console.log(res.data);
            res.data.ReturnData.forEach(function(obj, index) {
                list[index] = {
                    code: obj.RecordsTypeShow,
                    text: obj.GPSAddress_Tencent,
                    type: obj.Signtime.substr(11, 5)
                }
            });
            that.setData({ listData: list })
            console.log(list)
        }
    });
}